#include "StdAfx.h"
#include "Station.h"

using namespace Database;

StationRow::StationRow(void)
{
}

StationRow::~StationRow(void)
{
}

std::string StationRow::getID() const
{
	return _id;
}

void StationRow::setID(const std::string &id)
{
	_id = id;
}

std::string StationRow::getGuid() const
{
	return _guid;
}

void StationRow::setGuid(const std::string &gd)
{
	_guid = gd;
}

std::string StationRow::getAddress() const
{
	return _address;
}

void StationRow::setAddress(const std::string &ne)
{
	_address = ne;
}

std::string StationRow::getDescription() const
{
	return _description;
}

void StationRow::setDescription(const std::string &dn)
{
	_description = dn;
}

StationTable::StationTable()
{
}

StationTable::~StationTable()
{
}

bool StationTable::insert(const StationRow &sr)
{
	MysqlExecute me;
	me.init(getLogin());

	std::string sql = "insert into station set "
		" id='" + sr.getID() + "',"
		" guid='" + sr.getGuid() + "',"
		" address='" + sr.getAddress() + "'"
		" description='" + sr.getDescription() + "'";

	return me.execute(sql);
}

bool StationTable::select(std::list<StationRow> &srs)
{
	if (getLogin() == NULL)
	{
		return false;
	}

	std::string sql = "select id, guid, address, description"
		" from station";

	MysqlExecute me;
	me.init(getLogin());

	if (!me.execute(sql))
	{
		return false;
	}

	MysqlResult mr;
	char **v = mr.next(me.result());

	while (v != NULL)
	{
		StationRow sr;

		int i = 0;
		sr.setID( toStr(v[i++]) );
		sr.setGuid( toStr(v[i++]) );
		sr.setAddress( toStr(v[i++]) );
		sr.setDescription( toStr(v[i++]) );

		v = mr.next();

		srs.push_back(sr);
	}

	return true;
}

bool StationTable::updateByID(const StationRow &sr)
{
	if (getLogin() == NULL)
	{
		return false;
	}

	MysqlExecute me;
	me.init(getLogin());

	std::string sql = "Update station set"
		" guid='" + sr.getGuid() + "'"
		" address='" + sr.getAddress() + "'"
		" description='" + sr.getDescription() + "'"
		" where id='" + sr.getID() + "'";

	return me.execute(sql);
}

bool StationTable::deleteByID(const StationRow &sr)
{
	if (getLogin() == NULL)
	{
		return false;
	}

	MysqlExecute me;
	me.init(getLogin());
	std::string sql;

	sql = "delete from station"
		" where id='" + sr.getID() + "'";

	return me.execute(sql);
}